﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BhServer;
using System.ServiceModel;

namespace BhClient
{
    public partial class Form1 : Form, IClientCallback
    {
        public Form1()
        {
            InitializeComponent();
            users = new BindingList<User>(list);
            listBoxControl1.DataSource = users;
        }
        BindingList<User> users;
        IServer proxy;
        User me;
        List<User> list = new List<User>();

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            try
            {
                string endPointAddr = "net.tcp://172.29.3.5:8000/Server";
                NetTcpBinding tcpBinding = new NetTcpBinding();
                tcpBinding.TransactionFlow = false;
                tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
                tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
                tcpBinding.Security.Mode = SecurityMode.None;

                EndpointAddress endpointAddress = new EndpointAddress(endPointAddr);

                Append("Attempt to connect to: " + endPointAddr);
                InstanceContext ctx = new InstanceContext(this);

                proxy = DuplexChannelFactory<IServer>.CreateChannel(ctx, tcpBinding, endpointAddress);
                me = proxy.LogIn(textEdit1.Text);
                GetUsers();
                listBoxControl1.Refresh();
            }
            catch { }
        }

        void GetUsers()
        {
            if (proxy == null)
                return;
            lock (users)
            {
                User[] usr = proxy.GetUsers();
                if (usr != null)
                {
                    this.Invoke(new Action(() =>
                    {
                        list.Clear();
                        list.AddRange(usr);
                    }));
                }
            }
        }
        void Append(string text)
        {
            if (meLog.InvokeRequired)
            {
                meLog.Invoke(new Action<string>(Append), text);
                return;
            }
            meLog.Text += text + "\n\r";
            meLog.Select(meLog.Text.Length, 0);
            meLog.ScrollToCaret();
        }

        public void CheckConnection()
        {
            
        }

        public void NotifyMessage(User from, string message)
        {
           Append(from.Name + ": " + message);
        }

        public void NotifyUserLeft(User from)
        {
            
        }

        public void UserLeft(string name)
        {
            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            User to = listBoxControl1.SelectedItem as User;
            if (to != null)
            {
                Append("Ja:" + meText.Text);
                proxy.SendMessage(me, to, meText.Text);
            }
        }

        public void UserConnected(User user)
        {
            Append("**** " + user.Name + " connected ****");
            GetUsers();
        }

        public void UserDisconnected(User user)
        {
            GetUsers();
            Append("**** " + user.Name + " disconnected ****");
        }
    }
}
